﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.IO;

namespace RadarMonitor.DB
{
    /// <summary>
    /// 数据管理；数据库连接
    /// </summary>
    public partial class DBHelper
    {
        #region 单例
        private static readonly Lazy<DBHelper> lazy = new Lazy<DBHelper>(
            () => new DBHelper()
            );

        public static DBHelper Instance { get { return lazy.Value; } }

        private DBHelper()
        {

        }
        #endregion

        public static string BaseFTPTabel = "[dbo].[Table_BaseFtp]";
        public static string MonidorTabel = "[dbo].[Table_Monidor]";
        public static string ProductsMonitorTabel = "[dbo].[Table_Products]";
        public static string CIMISSTabel = "[dbo].[Table_BaseCimiss]";
        public static string PingTabel = "[dbo].[Table_Net]";
        public static string RdaTabel = "[dbo].[Table_Rda]";

        public static SqlConnection Connection { get => Instance.localDbCon.Value; }

        private ThreadLocal<SqlConnection> localDbCon = new ThreadLocal<SqlConnection>(() =>
        {
#if DEBUG
            // 调试时使用固定的数据库
            //var dbPath = @"D:\RadarMonitor\radar-monitor\RadarMonitor\DB\Radar.mdf";
            var dbPath = @"C:\Users\ThinkPad\source\repos\RadarMonitor\RadarMonitor\DB\Radar.mdf";


#else
            var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var pat = System.IO.Path.GetDirectoryName(location);
            var dbPath = System.IO.Path.Combine(pat, "DB", "Radar.mdf");
#endif
            var conStr = $@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ={dbPath}; Integrated Security = True; Connection Timeout=15";
            return new SqlConnection(conStr);
        });

        public string ConnectionString()
        {
#if DEBUG
            // 调试时使用固定的数据库
            //var dbPath = @"D:\RadarMonitor\radar-monitor\RadarMonitor\DB\Radar.mdf";
            var dbPath = @"C:\Users\ThinkPad\source\repos\RadarMonitor\RadarMonitor\DB\Radar.mdf";

#else
            var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var pat = System.IO.Path.GetDirectoryName(location);
            var dbPath = System.IO.Path.Combine(pat, "DB", "Radar.mdf");
#endif
            var conStr = $@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ={dbPath}; Integrated Security = True; Connection Timeout=15";
            return conStr;
        }

        /// <summary>
        /// 从代码创建数据库；安装包默认带数据库，数据库丢失的时候需要此功能
        /// </summary>
       
    }
}

/*
CREATE TABLE [dbo].[Table_BaseFtp] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [Time]       DATETIME       CONSTRAINT [DF_Table_BaseFtp_Time] DEFAULT (getdate()) NULL,
    [LastedFile] NVARCHAR (256) NULL,
    [ResultCode] INT            DEFAULT ((0)) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
*/


/*
 CREATE TABLE [dbo].[Table_Monidor] (
    [Id]        INT            IDENTITY (1, 1) NOT NULL,
    [Source]    INT            NULL,
    [Time]      DATETIME       CONSTRAINT [DF_Table_Monidor_Time] DEFAULT (getdate()) NULL,
    [AlarmCode] INT            DEFAULT ((0)) NOT NULL,
    [Msg]       NVARCHAR (256) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
*/